var scrollObj_endPalteInfo_page=null;
$( document ).on( "pagecreate", "#endPalteInfo_page", function( event ) {
	var toEndPlateId=getParameterByName("toEndPlateId", getLocationUrl(event) );
	loadData_endPlateInfo(toEndPlateId);
	//点击评论按钮
	$("#paltePublishBut").on("click",function (){
		var c = $(".publish").css("display");
		$("[name='publishCon']").val("");
		if("none" == c){
			$(".publish").css({"display":"block"});
		}else{
			$(".publish").css({"display":"none"});
		}
	});
	
	$("#wrapper_endPalteInfo_page  #pullDown").hide();
	scrollObj_endPalteInfo_page=scrollPlugLoadedOfReturn("wrapper_endPalteInfo_page",
			function() {
				setTimeout(function () {
					if(pageNo >= totalPage)
						return;
					pageNo++;
					loadData_comments(false,toEndPlateId,scrollObj_endPalteInfo_page);
				}, 1000);	
			},
			function() {
				scrollObj_endPalteInfo_page.refresh();
			});
	//加载评论列表
	reLoadReviewInfo_comments(toEndPlateId);
	//点赞
	$("#palteAgreeBut").on("click",function (){
		var type = $(this).attr("type");
		if('0'==type){
			click_agree(toEndPlateId,$(this))
		}else{
//			alert_wx('fail',"已经赞过了！");
		}
		
	});
	// 发表评论
	$("#publishBut").on("click",function (){
		var content = $("[name='publishCon']").val();
			publish_comments(toEndPlateId,content);
	});
});
//加载评论列表
function reLoadReviewInfo_comments(toEndPlateId){
	pageNo="1";
	pageSize="10";
	loadData_comments(true,toEndPlateId,scrollObj_endPalteInfo_page);
}
//吐槽信息
function loadData_endPlateInfo(toEndPlateId){
	$.ajax({
	  	url: rootPath+"/endplateReview/getEndPlateInfoById.do",
	  	type: "POST",
	  	dataType: "text",
		data: "access_token="+access_token+"&toEndPlateId="+toEndPlateId,
	  	success: function( response ) {
	  		var data=$.parseJSON(response);
			if(data.state=="200"){
				var headerPic=rootPath+defaultHeadPic;
				var review = data.data;
				var empName="匿名";
				if(review.type!=undefined && review.type=="0"){
					empName=review.emp_name;
					if(review.headImageUrl!=undefined &&review.headImageUrl!=""){
						headerPic=review.headImageUrl;	
					}
				} 
				$("#headPicImg").attr("src",headerPic).show();
				$("#empName_endPalteInfo").text(empName);
				$("#time_endPalteInfo").text(review.create_timeStr);
				//$("#title_endPalteInfo").text("#积分规划#");
				$("#explain_endPalteInfo").html(replaceContent(review.endPlateCon));
				//if(null!=review.praiseNum){ //判断是否已经赞过
				//	$("#palteAgreeBut").html("<img src=\"../../images/weixin/flower/icon_3.png\"/>已赞");
				//}
				$("#praiseNum_endPalteInfo").text((review.praiseNum==null?'0':review.praiseNum)+"人觉得很赞");
			}else{
				alert_wx("fail",data.msg);
			}
	  	}
	});
}
//加载评论列表
function loadData_comments(isFresh,toEndPlateId,scrollObj_endPalteInfo_page){
	$.ajax({
	  	url: rootPath+"/endplateReview/EPReviewList.do",
	  	type: "POST",
	  	dataType: "text",
		data: "access_token="+access_token+"&pageNo="+pageNo+"&pageSize="+pageSize+"&endPlateId="+toEndPlateId,
	  	success: function( response ) {
	  		var data=$.parseJSON(response);
			if(data.state=="200"){
				totalPage=data.data.totalPage;
		  		var totalRow=data.data.totalRow;
		  		var arr=data.data.rows;
				var content = "";
				for (var i=0;i<arr.length;i++){
					content+="<li data-icon='false'><a href='javascript:;'>"+arr[i].emp_name+"：<span>"+arr[i].eprCon+"</span></a></li>";
				}
				if(arr.length<pageSize){
					$("#wrapper_endPalteInfo_page  #pullUp").hide();
				}else{
					$("#wrapper_endPalteInfo_page  #pullUp").show();
				}
				if(isFresh && $("#thelist_endPalteInfo").html()!=""){
					$("#thelist_endPalteInfo").html("");
				}
				$("#thelist_endPalteInfo").append(content).listview('refresh');
				
				scrollObj_endPalteInfo_page.refresh();
				
			}else{
				alert_wx('fail',data.msg);
			}
	  	}
	});
}
//点赞
function click_agree(toEndPlateId,obj){
	$.ajax({
	  	url: rootPath+"/endplateReview/updatePraiseNum.do",
	  	type: "POST",
	  	dataType: "text",
		data: "access_token="+access_token+"&toEndPlateId="+toEndPlateId+"&praiseNum=1",
	  	success: function( response ) {
	  		var data=$.parseJSON(response);
			if(data.state=="200"){
				obj.attr("type","1");
				$("#palteAgreeBut").html("<img src='"+rootPath+"/images/weixin/flower/icon_3.png'/>已赞");
				var num=data.msg;
				if(!isNullOrEmpty(num)){
					$("#praiseNum_endPalteInfo").text(num+"人觉得很赞");
				}
			}else{
				alert_wx('fail',data.msg);
			}
	  	}
	});
}
//发表评论
function publish_comments(toEndPlateId,content){
	if(isNullOrEmpty(toEndPlateId)){
		alert_wx('fail',"页面错误，请刷新页面！");
		return false;
	}
	if(isNullOrEmpty(content)){
		alert_wx('fail',"评论内容不能为空！");
		return false;
	}else if(content.length>20){
		alert_wx('fail',"评论字数不能多于20！");
		return false;
	}
	$.ajax({
	  	url: rootPath+"/endplateReview/addEPReview.do",
	  	type: "POST",
	  	dataType: "text",
		data: "access_token="+access_token+"&toEndPlateId="+toEndPlateId+"&criticsType=0&content="+content,
	  	success: function( response ) {
	  		var data=$.parseJSON(response);
			if(data.state=="200"){
				alert_wx('success',"发表成功！");
				$(".publish").hide();
				reLoadReviewInfo_comments(toEndPlateId);
			}else{
				alert_wx('fail',data.msg);
			}
	  	}
	});
}
			